home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / twinopus / dopus / copyfile.rexx next >
Encoding:
OS/2 REXX Batch file  |  1995-07-13  |  14.4 KB  |  513 lines

  1. /*
  2.  *
  3.  * Copy file(s) with TwinExpress from DOpus.
  4.  *
  5.  * (c) 1994 by K.P. van Beem (2:280/464.2, patrick.van.beem@aobh.xs4all.nl)
  6.  *
  7.  * Based on the DOpusLhaARexx package by Geoff Seeley (but you can better
  8.  * use GuiArc in stead of DOpus and a script, to deal with archives)
  9.  *
  10. ------   some mods by Ray Abram 
  11.           -last update   24 / 12 / 94
  12.  
  13.     - properly handle Twin Dirs <-> DOpus dirs
  14.         - if a Dir is copied, then the dir is created,
  15.           and the files then copied into the directory...
  16.  
  17.     - All Directories are also recursed into...
  18.     - If Dopus is used as the Destination, then Dopus will be told to
  19.       re-scan the dir.
  20.  
  21.     - Add Wait option on REXX call command line
  22.         - this option (specified on the Command line) will force DOpus to wait
  23.           for each file to be copied before telling TWIN to copy the next...
  24.         -  eg rexx:dopus/CopyFile.rexx WAIT
  25.         - TRUE or False must be entered if you want to use "SAVEAS" option
  26.         - will report an error if the destination cannot fit the sent file
  27.         - when Dirs are copied, then twin reports as each file is copied
  28.             - so show these files as they are copied...
  29.  
  30.     - Add "Copy AS" ability
  31.         - option called "COPYAS" will prompt for a new name
  32.  
  33.     - Add a move command
  34.         - option call "DELETE"
  35.         - deletes the source after doing a copy
  36.         - *** note that the source window doen't get updated, thus looking like
  37.               a copy operation has been performed... (but the move has worked)
  38.  
  39.     - A bit of recognition on file coping. Now states Local or Remote on the
  40.       status line...
  41. */
  42.  
  43. /* trace results with user ENTER key to execute each command */
  44. Trace ?Results
  45.  
  46. DOpusPort   = 'DOPUS.1'
  47.  
  48. if ~show(l,"rexxsupport.library") then
  49.     call addlib("rexxsupport.library",0,-30,0)
  50. if showlist('Ports', DOpusPort) = 0 then do
  51.    say 'Directory Opus Arexx port not found. Aborting.'
  52.    call CleanUp
  53. end
  54.  
  55. address 'DOPUS.1'
  56. options results
  57.  
  58. /* get parameters and default all to off*/
  59. parse arg params
  60. Wait=False
  61. CopyAs=False
  62. Delete=False
  63.  
  64. /* now scan the options */
  65. do wrd = 1 to words(params)
  66.    temp = word(params, wrd)
  67.  
  68.    /*see if wait flag is here */
  69.    if upper(temp) = 'WAIT' then
  70.       Wait=True
  71.  
  72.    /* see if copy flag is here */
  73.    if upper(temp) = 'COPYAS' then
  74.       CopyAs=True
  75.  
  76.    /* see if delete flag is here */
  77.    if upper(temp) = 'DELETE' then
  78.       Delete=True
  79.  
  80. end
  81.  
  82. /* setup DOpus window and tell user what's happening */
  83. Busy on
  84. TopText "Copying selected files..."
  85.  
  86. /* Get the destination path */
  87. OtherWindow
  88. 'Status 6 -1'
  89. GetEntry Result
  90. ToPath = Result
  91. Dopus=False
  92. if left(ToPath,1) = '*' then do
  93.    ToPath = SubStr(ToPath,2)
  94.    DoReread='TRUE'
  95. end
  96. else do
  97.    'Status 13 -1'
  98.    ToPath = result
  99.    if right(ToPath,1) = '/' then
  100.       ToPath = left(ToPath,length(ToPath)-1) /* strip the / that DOPus put there */
  101.  
  102.    Dopus = True
  103.  
  104.    if ToPath = '' then do
  105.       TopText "No destination directory selected ???"
  106.       call CleanUp
  107.    end
  108.    DoReread='FALSE'
  109. end
  110.  
  111. OtherWindow
  112.  
  113. /* Get the current path and do file-copy, depending on the  */
  114. /* type of path (Twin-path or normal path)                  */
  115. 'Status 6 -1'
  116. GetEntry Result
  117. FilePath = Result
  118.  
  119. /* Twin-way (from twin to Dopus  or  from Twin to Twin)*/
  120. if left(FilePath,1) = '*' then do
  121.    FilePath = SubStr(FilePath,2)
  122.  
  123.    /* change to the source and destination dirs and remember local/remote bit */
  124.    SA = EnterDir(FilePath)
  125.    DA = EnterDir(ToPath)
  126.  
  127.    /*ensure that user only copies local -> remote or vise versa*/
  128.    if SA = '~' & DA = '~' then do
  129.       Request "Cannot Copy Remote -> Remote (Only Local <-> Remote !!)"
  130.       call CleanUp
  131.    end
  132.    if SA = '' & DA = '' then do
  133.       Request "Cannot Copy Local -> Local (Only Local <-> Remote !!)"
  134.       call CleanUp
  135.    end
  136.  
  137.    GetSelectedAll
  138.    SelectedEntries = result
  139.    if SelectedEntries = 'RESULT' then do
  140.       TopText "No Files/Dirs are selected."
  141.       call CleanUp
  142.    end
  143.    NumberOfEntries = words(SelectedEntries)
  144.    do EntryNumber = 1 to NumberOfEntries
  145.       Index = word(SelectedEntries, EntryNumber)
  146.       SelectEntry Index 0 1
  147.       GetEntry Index+1
  148.       Entry = result
  149.       ScrollToShow '"'Entry'"'
  150.       File = strip(left(Entry,25))
  151.  
  152.       if CopyAs = True then do
  153.          getstring  '"Enter New name for Moved File :"' File
  154.          File2 = Result
  155.          if File2="" then do
  156.             File2 = File
  157.             Request "No Rename Performed... (keeping" File2 ")"
  158.          end
  159.       end
  160.       else
  161.          File2 = File
  162.  
  163.       ToFile = Quote(DA || File2)
  164.       File   = Quote(SA || File)
  165.  
  166.       if words(File) > 1 then
  167.          Request "Spaces in File/Dir Names are not Allowed !!"
  168.       else do
  169.  
  170.          /* if source is a directory, then make a dir. in dest. */
  171.          extra = ''
  172.          Directory = upper(substr(Entry,26,1))
  173.          if Directory = 'D' then do
  174.             address command 'echo >PPipe: md' File
  175.             extra = ' SUBDIR'
  176.          end
  177.  
  178.          /* keep user updated with the file being copied */
  179.          if DA ~= '~' then
  180.             TopText "Copying Remote To Local " File
  181.          else
  182.             TopText "Copying Local to Remote " File
  183.  
  184.          /*tell TWIN to copy the File...  and then put something into the Queue:*/
  185.          address command 'echo >PPipe: copy' File ToFile Extra
  186.          address command 'echo >PPipe: echo END-LIST'
  187.          address command 'echo >PPipe: help'
  188.  
  189.          /* if user wanted to wait between files...*/
  190.          if Wait = True then
  191.             if open(PipeList, "QUEUE:Twin", 'R') then do
  192.                if extra = '' then
  193.                   call WaitForAFile
  194.                else do
  195.                   test = ''
  196.  
  197.                   /* received is for a normal copy,   sent is for a copy as */
  198.                   do while ( (find(junk,"END-LIST") = 0) & (find(junk,"Error:") = 0) )
  199.                      junk = readln(PipeList)
  200.                      test = left(junk , 8, '')
  201.                      if ((test = "received") | (test = "   sent ") | (test = " copied ")) then
  202.                         TopText "Copied file" word(junk, words(junk)) "..."
  203.                   end
  204.  
  205.                   /*check for errors and show user the error*/
  206.                   fnd = find(junk,"Error:")
  207.                   if fnd > 0 then
  208.                      Request right(junk,length(junk)-wordindex(junk,fnd)+1)
  209.                end
  210.                close(PipeList)
  211.             end
  212.  
  213.          /* if user wanted to delete files */
  214.          if Delete = True then do
  215.             TopText "Deleting" File "..."
  216.  
  217.             address command 'echo >PPipe: delete' File ' SUBDIR'
  218.             address command 'echo >PPipe: help'
  219.             if open(PipeList, "QUEUE:Twin", 'R') then do
  220.                test = ''
  221.                /* deleted is for a file just deleted */
  222.                do while (test ~= "deleted")
  223.                  junk = readln(PipeList)
  224.                  test = right(junk , 7, '')
  225.                end
  226.                close(PipeList)
  227.             end
  228.          end
  229.       end
  230.    end
  231.  
  232.    if ((Dopus = True) & (Delete = False)) then do
  233.       otherwindow
  234.       ReScan
  235.       otherwindow
  236.    end
  237.  
  238. end
  239.  
  240. /* Normal way (dopus -> twin, Dopus -> Dopus)*/
  241. else do
  242.  
  243.    'Status 13 -1'
  244.  
  245.    FilePath = result
  246.    if FilePath = '' then do
  247.       TopText "No Source directory selected. ??"
  248.       call CleanUp
  249.    end
  250.  
  251.    /*see if the destination is also a dopus dir */
  252.    otherwindow
  253.    'Status 6 -1'
  254.    GetEntry Result
  255.    FP = Result
  256.    otherwindow
  257.    if left(FP,1) ~= '*' then do
  258.       Busy off
  259.       if CopyAs = True then do
  260.          if Delete = True then do
  261.             MoveAs
  262.          end
  263.          else do
  264.             /* the CopyAs command is here, but doesn't seem to work !!?? */
  265.             CopyAs
  266.          end
  267.       end
  268.       else do
  269.          if Delete = True then do
  270.             Move
  271.          end
  272.          else do
  273.             Copy
  274.          end
  275.       end
  276.       call CleanUp
  277.    end
  278.  
  279.    /* change to the source and destination dirs and remember local/remote bit */
  280.    SA = EnterDir(FilePath)
  281.    DA = EnterDir(ToPath)
  282.  
  283.    /*ensure that user only copies local -> remote or vise versa*/
  284.    if SA = '~' & DA = '~' then do
  285.       Request "Cannot Copy Remote -> Remote (Only Local <-> Remote !!)"
  286.       call CleanUp
  287.    end
  288.    if SA = '' & DA = '' then do
  289.       Request "Cannot Copy Local -> Local (Only Local <-> Remote !!)"
  290.       call CleanUp
  291.    end
  292.  
  293.    do loop=1 to 2
  294.  
  295.       /* copy Dirs on Pass 1 and Files on pass 2 */
  296.       if loop=1 then do
  297.          extra = ' SUBDIR'
  298.          TopText "Copying Dirs"
  299.          'GetSelectedDirs "|" -1'
  300.       end
  301.       else do
  302.          extra = ''
  303.          TopText "Copying Files"
  304.          'GetSelectedFiles "|" -1'
  305.  
  306.       end
  307.  
  308.      /* do the following if some entries are selected */
  309.       SelectedEntries = result
  310.       if ~(SelectedEntries = 'RESULT') then do
  311.          NumberOfEntries = CountWords(SelectedEntries)
  312.          do EntryNumber = 1 to NumberOfEntries
  313.             File   = GetWord(EntryNumber, SelectedEntries)
  314.             SelectFile File 0 1
  315.             ScrollToShow '"'File'"'
  316.  
  317.             if CopyAs = True then do
  318.                getstring  '"Enter New name for Moved File :"' File
  319.                File2 = Result
  320.                if File2="" then do
  321.                   File2 = File
  322.                   TopText "No Rename Performed... "
  323.                end
  324.             end
  325.             else
  326.                File2 = File
  327.  
  328.             ToFile = Quote(DA || File2)
  329.             File   = Quote(SA || File)
  330.  
  331.             if words(ToFile) > 1 then do
  332.                Request "Spaces in File/Dir Names are not Allowed !!"
  333.  
  334.             end
  335.             else do
  336.  
  337.                if loop = 1 then
  338.                   address command 'echo >PPipe: md' ToFile
  339.  
  340.                /* keep user updated with the file being copied */
  341.                if DA ~= '~' then
  342.                   TopText "Copying Remote To Local " File
  343.                else
  344.                   TopText "Copying Local to Remote " File
  345.  
  346.                /* get twin to do the copy... */
  347.                address command 'echo >PPipe: copy' File ToFile Extra
  348.                address command 'echo >PPipe: echo END-LIST'
  349.                address command 'echo >PPipe: help'
  350.  
  351.                /* if user wanted to wait between files...*/
  352.                if Wait = True then
  353.                   if open(PipeList, "QUEUE:Twin", 'R') then do
  354.                      if extra = '' then
  355.                         call WaitForAFile
  356.                      else do
  357.                         test = ''
  358.  
  359.                         /* received is for a normal copy,   sent is for a copy as */
  360.                         do while ( (find(junk,"END-LIST") = 0) & (find(junk,"Error:") = 0) )
  361.                            junk = readln(PipeList)
  362.                            test = left(junk , 8, '')
  363.                            if ((test = "received") | (test = "   sent ") | (test = " copied ")) then
  364.                               TopText "Copied file" word(junk, words(junk)) "..."
  365.                         end
  366.  
  367.                         /*check for errors and show user the error*/
  368.                         fnd = find(junk,"Error:")
  369.                         if fnd > 0 then
  370.                            Request right(junk,length(junk)-wordindex(junk,fnd)+1)
  371.                      end
  372.                      close(PipeList)
  373.                   end
  374.  
  375.                /* if user wanted to delete files */
  376.                if Delete = True then do
  377.                   address command 'echo >PPipe: delete' File ' SUBDIR'
  378.                   address command 'echo >PPipe: help'
  379.                   if open(PipeList, "QUEUE:Twin", 'R') then do
  380.                      test = ''
  381.                      /* deleted is for a deleted file */
  382.                      do while (test ~= "deleted")
  383.                        junk = readln(PipeList)
  384.                        test = right(junk , 7, '')
  385.                      end
  386.                      close(PipeList)
  387.                   end
  388.                end
  389.             end
  390.          end
  391.       end  /* for */
  392.    end     /* if nothing is selected */
  393.  
  394.    if Dopus = True then do
  395.       otherwindow
  396.       ReScan
  397.       otherwindow
  398.    end
  399. end
  400.  
  401. EndBit:
  402.  
  403. TopText "Ready"
  404. 'DisplayDir -1'
  405. if ((DoReread = True) | (Dopus = True)) then do
  406.    otherwindow
  407.    address AREXX "Rexx:DOpus/ReRead.rexx"
  408. end
  409.  
  410. if Delete = True then
  411.    request "Please ReRead the Source Directory..."
  412.  
  413. call CleanUp
  414.  
  415. exit
  416.  
  417. /*---------------------------------------------------------------------------*/
  418.  
  419. CleanUp: /* Remove any files and exit */
  420.    Busy off
  421.    DopusToFront
  422.    exit
  423. return
  424.  
  425. /*---------------------------------------------------------------------------*/
  426.  
  427. WaitForAFile:
  428.    test = ''
  429.    /* received is for a normal copy,   sent is for a copy as */
  430.    do while ((test ~= "received") & (test ~= "   sent ") & (test ~= " copied ") & (test ~= "Error: D") & (test ~= "TWIN> Er"))
  431.      junk = readln(PipeList)
  432.      test = left(junk , 8, '')
  433.    end
  434.  
  435.    /*check for errors and show user the error*/
  436.    fnd = find(junk,"Error:")
  437.    if fnd > 0 then do
  438.       Request right(junk,length(junk)-wordindex(junk,fnd)+1)
  439.       close(PipeList)
  440.       call EndBit
  441.    end
  442.  
  443. return
  444.  
  445. /*--------------------------------------------------------------------------*/
  446.  
  447. Quote: procedure /* add quotes to string */
  448.    parse arg string
  449. return '"'||string||'"'
  450.  
  451. /*--------------------------------------------------------------------------*/
  452.  
  453. GetWord: procedure /* get word from '|' separated string */
  454.  
  455.   parse arg number,words
  456.  
  457.   if(left(words,1) ~= '|') then
  458.      words = '|'||words
  459.   do i=1 to number
  460.      idx = index(words, '|');
  461.      words = substr(words, idx+1)
  462.   end
  463.   end = index(words, '|') - 1
  464.   if words = "" then
  465.      return ""
  466.  
  467.   ret_str = substr(words, 1, end)
  468. return ret_str
  469.  
  470. /*--------------------------------------------------------------------------*/
  471.  
  472. CountWords: procedure /* count words from '|' separated string */
  473.  
  474.    parse arg words
  475.  
  476.    count = 0
  477.    idx = index(words, '|')
  478.    do while idx ~= 0
  479.      count = count + 1
  480.      words = substr(words, idx+1)
  481.      idx = index(words, '|')
  482.    end
  483. return count
  484.  
  485. /*--------------------------------------------------------------------------*/
  486.  
  487. /* Cd into the sent Directory path
  488.    - this is needed, as TWIN has a command parameter limit of 30 characters,
  489.       and as we all know AmigaDos file & paths can easily go over this limit !!*/
  490.  
  491. EnterDir: procedure
  492.  
  493.    parse arg Dev ':' Path
  494.  
  495.    /* get the 1st character to ddress the local or remote machine correctly */
  496.    if left(Dev,1) = '~' then
  497.       sbit = '~'
  498.      else
  499.       sbit = ''
  500.  
  501.    /* does the passed name have a DEVICE in it ? */
  502.    if Dev ~= '' then
  503.       address command 'echo >PPipe: cd' Dev || ':'
  504.  
  505.    do until (t2 = '')
  506.       parse var Path t1 '/' t2
  507.       path = t2
  508.       if t1 ~= '' then
  509.          address command 'echo >PPipe: cd' sbit || t1
  510.    end
  511.  
  512. return sbit
  513.